import us.versus.them.glassifried.Button;
import us.versus.them.glassifried.ProgressBar;
import us.versus.them.glassifried.Slider;

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

class Examples {

	var red:Button;
	var yellow:Button;
	var green:Button;

	var progress:Float;
	var progressBar:ProgressBar;

	public function new() {
		var clip = flash.Lib.current;

		this.buttonUp( clip );
		this.howProgressive( clip );
		this.slideItIn( clip );
	}

	public function buttonUp( clip ) {

		clip.addChild( red    = new Button( 10,  10, 255,0,0,   'Stop'   ,  action ) );
		clip.addChild( yellow = new Button( 10,  60, 255,255,0, 'Caution',  action ) );
		clip.addChild( green  = new Button( 10, 110, 0,255,0,   'Go'     ,  action ) );

		// simple centering...
		green.x += ( yellow.width - green.width ) / 2;
		red.x   += ( yellow.width - red.width ) / 2;

		clip.addChild( new Button( 220,  20, 72,72,255, ' ◀ ', action ) );
		clip.addChild( new Button( 270,  20, 72,72,255, ' ☠ ', action ) );
		clip.addChild( new Button( 320,  20, 72,72,255, ' ▶ ', action ) );
		clip.addChild( new Button( 200, 123, 255,72,255, 'Brian ♥ Karina', action ) );
	}

	public function howProgressive( clip ) {
		this.progress = 0.0;
		this.progressBar = new ProgressBar( 20,200, 300,30, 40,164,164, ProgressBar.percent );
		clip.addChild( this.progressBar );
		flash.Lib.current.addEventListener( Event.ENTER_FRAME, enter_frame );
	}

	public function slideItIn( clip:MovieClip ) {
		var hslider = new Slider( 20, 250, 300, 30, 164,40,40 );
		var vslider = new Slider( 340,200,  30, 100, 164,40,40 );
		clip.addChild( hslider );
		clip.addChild( vslider );

		var w = 200;
		var h = 40;
		var tmp_w = 0.0;
		var tmp_h = 0.0;

		var slick = new flash.display.Sprite();
		slick.x = 20;
		slick.y = 290;
		clip.addChild( slick );

		var draw = function() {
			slick.graphics.clear();
			slick.graphics.lineStyle( 2, 0 );
			slick.graphics.drawRect( 0, 0, w, h );

			slick.graphics.beginFill( 0xF00000 );
			slick.graphics.drawRect( 0, 0, tmp_w, tmp_h );
			slick.graphics.endFill();
		};

		draw();

		hslider.addEventListener( Event.CHANGE
			, function( e:SliderEvent ) { 
				tmp_w = w * e.percentage;
				draw();
			}
		);
		vslider.addEventListener( Event.CHANGE
			, function( e:SliderEvent ) { 
				tmp_h = h * e.percentage;
				draw();
			}
		);

		hslider.move( 0.5 );
		vslider.move( 0.5 );
	}

	public function action( what:String, mouseEvent : MouseEvent ) {
		switch( what ) {
			case 'Stop':    green.disable();  yellow.enable();
			case 'Caution': red.disable();    green.enable();
			case 'Go':      yellow.disable(); red.enable();
		}
	}

	var foo:Int;

	public function enter_frame( event : Event ) {
		this.progress += Math.random() * 0.001;
		if ( this.progress > 1 ) this.progress = 0;
		this.progressBar.move( this.progress );
	}

	static function main() {
		new Examples();
	}
}
